﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FazaDemo.ViewModels.zlib;

namespace FazaDemo.ViewModels
{
    public class Converter
    {
         internal static List<Fl> Flds = new List<Fl>();

private static void InitQbe(string qbe)
        {
            Flds.Clear();
            string MName = "";
            string MFrom = "";
            string MDelete = "";
            string MUpdate = "";
            string MJoin = "";
            string MWhere = "";
            string MOrder = "";
            string MGenerator = "";
            bool MDistinct = false;
            // ??????? ?? ???? ???? QBE
            string z = " ";
            int klvFld = 0;

            var cm = new SqlCommand("SELECT COALESCE(ECQ,' ') FROM TS_SYS_ECQ WHERE ID=@qbe") { Connection = null };
            cm.Parameters.AddWithValue("qbe", qbe);
            var dr = cm.ExecuteReader();
            while (dr.Read()) { z = dr.GetString(0); }
            cm.Dispose();
            dr.Close(); dr.Dispose();
            if (z != " ")
            {
                z = z.Replace("\\" + '\r' + '\n', "");
                //??????? ???? QBE ? ???????
                var spl = z.Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i <= spl.Length - 1; i++)
                {
                    var x = spl[i].ToUpper();
                    if (MName == "") { GetSprString(x, "[NAME]", out MName); }
                    if (MFrom == "") { GetSprString(x, "[FROM]", out MFrom); }
                    if (MDelete == "") { GetSprString(x, "[DELETE]", out MDelete); }
                    if (MUpdate == "") { GetSprString(x, "[UPDATE]", out MUpdate); }
                    if (MJoin == "") { GetSprString(x, "[JOIN]", out MJoin); }
                    if (MWhere == "") { GetSprString(x, "[WHERE]", out MWhere); }
                    if (MOrder == "") { GetSprString(x, "[ORDER]", out MOrder); }
                    if (MGenerator == "") { GetSprString(x, "[GENERATOR]", out MGenerator); }
                    if (x.Contains("[FIELD]"))
                    {
                        Flds.Add(new Fl(x.Substring(8, x.Length - 9), klvFld, 0));
                        klvFld++;
                    }
                    if (x.Contains("[DISTINCT]")) { MDistinct = true; }
                }
                if (MName == "") { MName = "NUM_ECQ=" + qbe; }
            }
        }

       

        private static void GetSprString(string y, string str, out string nm)
        {
            nm = "";
            if (y.Length > str.Length)
            {
                if (y.Contains(str))
                {
                    nm = y.Substring(str.Length + 1, y.Length - (str.Length + 1)).Trim();
                }
            }
        }
    }
           

    

namespace zlib
{
    public class Fl
    {
        public int X { get; private set; }
        public string Fld { get; set; }
        public string FldName { get; set; }
        public string FldType { get; set; }
        public bool FldShowTab { get; set; }
        public bool FldShowQuery { get; set; }
        public bool FldShowView { get; set; }
        public bool FldEdt { get; set; }
        public bool FldEdtEx1 { get; set; }
        public bool FldEdtEx2 { get; set; }
        public int FldOrder { get; set; }
        public bool FldSprav { get; set; }
        public int FldNumEcq { get; set; }
        public bool FldOut { get; set; }
        public bool FldKey { get; set; }
        /*
        public bool FldBold { get; set;}
        
        public string FldDef { get; set;}
        public bool FldFle { get; set;}
        public bool FldFla { get; set;}
        public bool FldFli { get; set;}
        public bool FldFlp { get; set;}
        public string FldPar { get; set;}
        public bool FldFlr { get; set;}
        public bool FldNulA { get; set; }
        public bool FldNulE { get; set;}
        public string FldCheck { get; set; }
        //*/
        public int Sort { get; set; }
        public int MaxSize { get; set; }
        public string[,] Condition { get; set; }
        

        public Fl()
        {
            Fld = "";
            FldName = "";
            FldType = "?";
            FldShowTab = false;
            FldShowQuery = false;
            FldShowView = false;
            FldEdt = false;
            FldEdtEx1 = false;
            FldEdtEx2 = false;
            FldOrder = 0;
            FldSprav = false;
            FldNumEcq = 0;
            FldOut = false;
            FldKey = false;
            /*
            FldBold = false;
            
            FldDef = "";
            FldFle = false;
            FldFla = false;
            FldFli = false;
            FldFlp = false;
            FldPar = "";
            FldFlr = false;
            FldNulA = false;
            FldNulE = false;
            FldCheck = "";
            //*/
            Sort = 0;
            MaxSize = 0;
            Condition = new string[1, 3] { { "??? ???????", "", "?" } };
            X = Condition.GetLength(0);
        }

        public Fl(string sss, int i, int v)
        {
            sss = sss.Replace("\\", "").ToUpper();
            var s = sss.Split(';');
            if (s[9].Trim() == "")
            {
                s[9] = "0";
            }
            Fld = s[0].Trim();
            FldName = s[1].Trim();
            FldType = s[2].Trim();
            FldShowTab = (s[3].Trim() != "NOSHOWTAB");
            FldShowQuery = (s[4].Trim() != "NOSHOWQUERY");
            FldShowView = (s[5].Trim() != "NOSHOWVIEW");
            FldEdt = (s[6].Trim() == "SHEDT" || s[6].Trim() == "SHEDTEX1" || s[6].Trim() == "SHEDTEX2");
            FldEdtEx1 = (s[6].Trim() == "SHEDTEX1" || s[6].Trim() == "SHEDTEX2");
            FldEdtEx2 = (s[6].Trim() == "SHEDTEX2");
            switch (s[7].Trim())
            {
                case "ORDER_1":
                    FldOrder = 1;
                    break;
                case "ORDER_2":
                    FldOrder = 2;
                    break;
                default:
                    FldOrder = 0;
                    break;
            }
            FldSprav = (s[8].Trim() == "SPRAV");
            FldNumEcq = (s[9].Trim() == "NOECQ" ? 0 : int.Parse(s[9].Trim()));
            FldOut = (s[10].Trim() == "OUT");
            FldKey = (s[11].Trim() == "KEYFIELD");
            /*
            FldBold = (s[12].Trim() == "FONTBOLD");
            
            FldDef = s[14].Trim();
            FldFle = (s[15].Trim().Contains("E"));
            FldFla = (s[15].Trim().Contains("A"));
            FldFli = (s[16].Trim().Contains("I"));
            FldFlp = (s[17].Trim().Length > 0);
            FldPar = s[17].Trim();
            FldFlr = (s[18].Trim() == "R");
            FldNulA = (s[19].Trim().Contains("A"));
            FldNulE = (s[19].Trim().Contains("E"));
            FldCheck = s[20].Trim();
            //*/
            Sort = 0;
            MaxSize = 0;

            switch (s[2].Trim())
            {
                case "N":
                    if (!FldSprav)
                    {
                        Condition = new string[11, 3]
                        {
                            {"??? ???????", "", "N"},
                            {"????? (=)", "@@@@ = $$$$", "N"},
                            {"?? ????? (#)", "@@@@ != $$$$", "N"},
                            {"?????? (>)", "@@@@ > $$$$", "N"},
                            {"?????? (<)", "@@@@ < $$$$", "N"},
                            {"?? ?????? (<=)", "@@@@ <= $$$$", "N"},
                            {"?? ?????? (>=)", "@@@@ >= $$$$", "N"},
                            {"?? ??????? (... , ...)", "@@@@ IN (####)", "N"},
                            {"??? ??????? (... , ...)", "@@@@ NOT IN (####)", "N"},
                            {"? ????????? (?? ... ?? ...)", "@@@@ BETWEEN !!!!", "N"},
                            {"??? ?????????", "@@@@ NOT BETWEEN !!!!", "N"}
                        };
                    }
                    else
                    {
                        Condition = new string[3, 3]
                        {
                            {"??? ???????", "", "N"},
                            {"????? (=)", "@@@@ = $$$$", "N"},
                            {"?? ????? (#)", "@@@@ != $$$$", "N"},
                        };
                    }
                    break;
                case "D":
                    if (!FldSprav)
                    {
                        Condition = new string[13, 3]
                        {
                            {"D_??? ???????", "", "D"},
                            {"D_????? (=)", "@@@@ = '$$$$'", "D"},
                            {"D_?? ????? (#)", "@@@@ != '$$$$'", "D"},
                            {"D_?????? (>)", "@@@@ > '$$$$'", "D"},
                            {"D_?????? (<)", "@@@@ < '$$$$'", "D"},
                            {"D_?? ?????? (<=)", "@@@@ <= '$$$$'", "D"},
                            {"D_?? ?????? (>=)", "@@@@ >= '$$$$'", "D"},
                            {"D_?? ??????? (... , ...)", "@@@@ IN (####)", "D"},
                            {"D_??? ??????? (... , ...)", "@@@@ NOT IN (####)", "D"},
                            {"D_? ????????? (?? ... ?? ...)", "@@@@ BETWEEN !!!!", "D"},
                            {"D_??? ?????????", "@@@@ NOT BETWEEN !!!!", "D"},
                            {"D_?????", "@@@@ IS NULL", "D"},
                            {"D_?? ?????", "@@@@ IS NOT NULL", "D"}
                        };
                    }
                    else
                    {
                        Condition = new string[3, 3]
                        {
                            {"D_??? ???????", "", "D"},
                            {"D_????? (=)", "@@@@ = '$$$$'", "D"},
                            {"D_?? ????? (#)", "@@@@ != '$$$$'", "D"},
                        };
                    }
                    break;
                case "C":
                    if (!FldSprav)
                    {
                        Condition = new string[15, 3]
                        {
                            {"??? ???????", "", "C"},
                            {"?????", "COALESCE(@@@@,'') = ''", "C"},
                            {"?? ?????", "COALESCE(@@@@,'') <> ''", "C"},
                            {"????????? ? ( = ?????)", "UPPER(@@@@) = UPPER('$$$$')", "C"},
                            {"?? ????????? (# ?????)", "UPPER(@@@@) != UPPER('$$$$')", "C"},
                            {"?????????? ( ????? ...)", "UPPER(@@@@) LIKE UPPER('$$$$%')", "C"},
                            {"?? ??????????", "UPPER(@@@@) NOT LIKE UPPER('$$$$%')", "C"},
                            {"???????? (... ????? ...)", "UPPER(@@@@) LIKE UPPER('%$$$$%')", "C"},
                            {"?? ????????", "UPPER(@@@@) NOT LIKE UPPER('%$$$$%')", "C"},
                            {"???????????? (... ?????)", "UPPER(@@@@) LIKE UPPER('%$$$$')", "C"},
                            {"?? ????????????", "UPPER(@@@@) NOT LIKE UPPER('%$$$$')", "C"},
                            {"?? ??????? (... , ...)", "@@@@ IN (####)", "C"},
                            {"??? ??????? (... , ...)", "@@@@ NOT IN (####)", "C"},
                            {"? ????????? (?? ... ?? ...)", "@@@@ BETWEEN !!!!", "C"},
                            {"??? ?????????", "@@@@ NOT BETWEEN !!!!", "C"}
                        };
                    }
                    else
                    {
                        Condition = new string[3, 3]
                        {
                            {"??? ???????", "", "C"},
                            {"?????", "COALESCE(@@@@,'') = ''", "C"},
                            {"?? ?????", "COALESCE(@@@@,'') <> ''", "C"},
                        };
                    }
                    break;
                case "B":
                    Condition = new string[3, 3]
                    {
                        {"??? ???????(L)", "", "B"},
                        {"??", "@@@@ = 1", "B"},
                        {"???", "@@@@ = 0", "B"}
                    };
                    break;
                case "F":
                    if (!FldSprav)
                    {
                        Condition = new string[11, 3]
                        {
                            {"??? ???????", "", "F"},
                            {"????? (=)", "@@@@ = $$$$", "F"},
                            {"?? ????? (#)", "@@@@ != $$$$", "F"},
                            {"?????? (>)", "@@@@ > $$$$", "F"},
                            {"?????? (<)", "@@@@ < $$$$", "F"},
                            {"?? ?????? (<=)", "@@@@ <= $$$$", "F"},
                            {"?? ?????? (>=)", "@@@@ >= $$$$", "F"},
                            {"?? ??????? (... , ...)", "@@@@ IN (####)", "F"},
                            {"??? ??????? (... , ...)", "@@@@ NOT IN (####)", "F"},
                            {"? ????????? (?? ... ?? ...)", "@@@@ BETWEEN !!!!", "F"},
                            {"??? ?????????", "@@@@ NOT BETWEEN !!!!", "F"}
                        };
                    }
                    else
                    {
                        Condition = new string[3, 3]
                        {
                            {"??? ???????", "", "F"},
                            {"????? (=)", "@@@@ = $$$$", "F"},
                            {"?? ????? (#)", "@@@@ != $$$$", "F"},
                        };
                    }
                    break;
                case "M":
                    if (!FldSprav)
                    {
                        Condition = new string[9, 3]
                        {
                            {"??? ??????? (M)", "", "M"},
                            {"????????? ? ( = ?????) (M)", "@@@@ = '$$$$'", "M"},
                            {"?? ????????? (# ?????) (M)", "@@@@ != '$$$$'", "M"},
                            {"?????????? ( ????? ...) (M)", "@@@@ LIKE '$$$$%'", "M"},
                            {"?? ?????????? (M)", "@@@@ NOT LIKE '$$$$%'", "M"},
                            {"???????? (... ????? ...) (M)", "@@@@ LIKE '%$$$$%'", "M"},
                            {"?? ???????? (M)", "@@@@ NOT LIKE '%$$$$%'", "M"},
                            {"???????????? (... ?????) (M)", "@@@@ LIKE '%$$$$'", "M"},
                            {"?? ???????????? (M)", "@@@@ NOT LIKE '%$$$$'", "M"}
                        };
                    }
                    else
                    {
                        Condition = new string[3, 3]
                        {
                            {"??? ??????? (M)", "", "M"},
                            {"????????? ? ( = ?????) (M)", "@@@@ = '$$$$'", "M"},
                            {"?? ????????? (# ?????) (M)", "@@@@ != '$$$$'", "M"},
                        };
                    }
                    break;
                default:
                    Condition = new string[1, 3] { { "??? ???????", "", "?" } };
                    break;
            }
            X = Condition.GetLength(0);

           
        }
    }
}

}
